<?php
/**
 * FunctionCommon
 * Use contain function common
 * @author GMO-RUNSYSTEM
 * @since 
 * $Id: $
 */
define('BASE_DIR', dirname(__FILE__));
class FunctionCommon
{
	 /*
     * Create Date:2013/08/20
     * Update Date:
     * Author:haipt
     * User change:
     * Description:change YYYY-mm-dd =>YYYY/mm/dd
     * */
     public function changeFormatDatetime($strtime)
     {
        $year=date('Y', strtotime($strtime));
		$month=date('n', strtotime($strtime));
		$date=date('j', strtotime($strtime));
        return $year."/".$month."/".$date;
     }
     /**
     * @this is crop text
     * @param model
     * @return 
     * @author Baodt
     * date: 24/07/2013
     */
    public function crop($text, $len) {
        $arr_replace = array("<p>", "</p>","<b>", "</b>","<br>", "<br />", "");
        $text = str_replace($arr_replace,"",$text);
        if ($len > strlen($text)) {
            $string = $text;
        } else {
            $string_cop = mb_substr($text, 0,$len,'UTF-8'); 
            $string = $string_cop;
        }
        return $string;
    }
    public function changeFormatDate($strtime)
    {
        $year=date("Y",strtotime($strtime));
        $month=date("m",strtotime($strtime));
        return $year."年".$month."月";
    }
	public function changeFormatDateFull($strtime)
    {
        $year=date("Y",strtotime($strtime));
        $month=date("m",strtotime($strtime));
		$day=date("d",strtotime($strtime));
        return $year."年".$month."月".$day."日";
    }
	
	
    public function getNumHour($strtime)
    {
        $hour=(strtotime("now")-strtotime($strtime))/3600;
        return floor($hour);
        
    }
    /*
	 * Create Date:
	 * Update Date:
	 * Author:Haipt
	 * User change:
	 * Description:read images from a foldel in server  
	* */
    function getImage($path,$file_name)
	{
		$file_path =$path.$file_name;
		$img_str="";
		if (is_readable($file_path) && $file_name !=null){
			$info = getimagesize($file_path);
			if ($info !== FALSE){
				$img_binary = fread(fopen($file_path, "r"), filesize($file_path));
				$img_str = base64_encode($img_binary); 
				return 	"data:".$info['mime'].";base64,".$img_str;
			}
		}
		return null;
	}
    function changeStringToYearMonth($strtime)
    {
        $year=substr($strtime,0,4);
        $month=substr($strtime,5,2);
        return $year."年".$month;
    }
    function getFileSize($filename)
    { 
       if(!empty($filename)){
            $path=PATH_FILE_FORSTORE.$filename;
            if(fileExistsInPath($path))
            {
                return round(filesize($path)/(1024*1024),2);
            }
       }
        
        return 0;
        
    }
    function getFileExt($filename)
    {
        if(!empty($filename))
        {
            preg_match('/\.[^\.]+$/i',$filename,$ext);
            return substr($ext[0],1,strlen($ext[0]));
        }
        else{
            return "";
        }
        
    }
    function checkFileExit($filename)
    {
        if(!empty($filename))
        {
            $path=PATH_FILE_FORSTORE.$filename;
            if(fileExistsInPath($path))
            {
                return true;
            } 
        }
        
        return false;
    }
    function getImageToHeightOrWidth($path,$file_name,$height,$width)
	{
		$file_path =$path.$file_name;
		$img_str="";
        if (is_readable($file_path) && $file_name !=null){
			$info = getimagesize($file_path);
            if ($info !== FALSE){
            list($width_orig, $height_orig) = getimagesize($file_path);
            if((!empty($width) && $width < $width_orig) || (!empty($height) && $height < $height_orig))
            {
                 $ratio_orig = $width_orig / $height_orig;
                if($width=="")
                {
                    $ratio = $height / $height_orig;
                    $width = $width_orig * $ratio;
                    
                }
                else if($height=="")
                {
                    $ratio = $width / $width_orig;
                    $height = $height_orig * $ratio;
                }
                $image_p = imagecreatetruecolor($width, $height);
                if ($info['mime'] == 'image/jpg' || $info['mime'] == 'image/jpeg') {
                    $image = imagecreatefromjpeg($file_path);
                } else if ($info['mime'] == 'image/png') {
                    $image = imagecreatefrompng($file_path);
                } else if ($info['mime'] == 'image/gif') {
                    $image = imagecreatefromgif($file_path);
                }
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                ob_start();
                if ($info['mime'] == 'image/jpg' || $info['mime'] == 'image/jpeg') {
                    imagejpeg($image_p);
                } else if ($info['mime'] == 'image/png') {
                    
                    imagepng($image_p);
                } else if ($info['mime'] == 'image/gif') {
                    imagegif($image_p);
                }
                
                    $img_str = base64_encode(ob_get_clean()); 
                    imagedestroy($image_p);
    				return 	"data:".$info['mime'].";base64,".$img_str;
    			}
            else{
                $img_binary = fread(fopen($file_path, "r"), filesize($file_path));
				$img_str = base64_encode($img_binary); 
				return 	"data:".$info['mime'].";base64,".$img_str;
            }
           
		}
        }
		return null;
	}
    function checkFileImgExit($path)
    {	
        
        $path=WWW_ROOT.$path;
        if(!empty($path))
        {
            $chxExist = is_file($path);
            if($chxExist)
            {
                return true;
            } 
        }
        
        return false;
    }
    
   
	public static function encode($url) 
	{
	    return preg_replace(array('/\+/', '/\//'), array('-', '_'), base64_encode($url));
    }
	
	public static function decode($url_encoded)
	{
	    return base64_decode(preg_replace(array('/-/', '/_/'), array('+', '/'), $url_encoded));
	}
    
    public function checkXMLString($xml)
    {
        libxml_use_internal_errors( true );
        $doc = new DOMDocument('1.0', 'utf-8');
       
        $doc->loadXML( $xml );
         
        $errors = libxml_get_errors();
         
        return empty( $errors );
    }
    function getSlideImage($path,$file_name,$height_new,$width_new)
    {
    	$file_path =$path.$file_name;
    	$img_str="";
    	$scare=$width_new/$height_new;
    	if (is_readable($file_path) && $file_name !=null){
    		$info = getimagesize($file_path);
    		if ($info !== FALSE){
    		list($width_orig, $height_orig) = getimagesize($file_path);
    		//co duoc chieu cao va chieu rong anh goc
    		// Tiep theo ta tinh ti le
    		$height=$height_new;
    		$ratio_height=$height/$height_orig;
    		if(!empty($height))
    		{
    			$ratio_orig = $width_orig / $height_orig;
    			$ratio = $height / $height_orig;
                $width = $width_orig * $ratio;
                $ratio_now=$width/$height;
                if($ratio_now > $scare)
                {
                	$width=$width_new;
                    $ratio = $width / $width_orig;
                	$height = $height_orig * $ratio;
                }
    				
    			$image_p = imagecreatetruecolor($width, $height);
    			if ($info['mime'] == 'image/jpg' || $info['mime'] == 'image/jpeg') {
    				$image = imagecreatefromjpeg($file_path);
    			} else if ($info['mime'] == 'image/png') {
    				$image = imagecreatefrompng($file_path);
    			} else if ($info['mime'] == 'image/gif') {
    				$image = imagecreatefromgif($file_path);
    			}
    			imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    			ob_start();
    			if ($info['mime'] == 'image/jpg' || $info['mime'] == 'image/jpeg') {
    				imagejpeg($image_p);
    			} else if ($info['mime'] == 'image/png') {
    				
    				imagepng($image_p);
    			} else if ($info['mime'] == 'image/gif') {
    				imagegif($image_p);
    			}
    			
    				$img_str = base64_encode(ob_get_clean()); 
    				imagedestroy($image_p);
    				return 	"data:".$info['mime'].";base64,".$img_str;
    		}
    				  
    		}
    	}
    	return null;
    }
    function getServerAdd()
    {
        if (isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != "off")) {
		 return "https://".$_SERVER['SERVER_NAME'];
    	} else {
    		 return "http://".$_SERVER['SERVER_NAME'];
    	}
    }
	
	
    //crop string follow number character
    public function cropNumChar($text, $len) {
        $arr_replace = array("<p>", "</p>","<b>", "</b>","<br>", "<br />", "");
        $text = str_replace($arr_replace,"",$text);
        if ($len > mb_strlen($text)) {
            $string = $text;
        } else {
            $string_cop = mb_substr($text, 0,$len,'UTF-8'); 
            $string = $string_cop."...";
        }
        return $string;
    }
    //send mail
    public static function sendMail($TO, $SUBJECT, $BODY, $FROM, $FROM_NAME=false, $TO_NAME=false) {
        $mailHeader  = "From:".mb_encode_mimeheader("{$FROM_NAME}"). "<".$FROM.">\n";
        $mailHeader.="Bcc:<".BCC_MAIL.">\n";
        $mailHeader.="Cc:<".CC_MAIL.">\n";
        $mailHeader.= "Content-Type: text/plain;charset=iso-8859-1 \n";
        $addHeader   = '-f'.$FROM;
        mb_internal_encoding("UTF-8");
        return mb_send_mail($TO, $SUBJECT, $BODY, $mailHeader, $addHeader);
     }
    

}
